import json

from douyuDataShow.models import DouYuDataModel

from django.shortcuts import render
from django.http import JsonResponse
from django.views import View


# Create your views here.


class DouYuDataCenter(View):
    def get(self, request):
        return JsonResponse({"msg": "hello"})

    def post(self, request):
        datas = json.loads(request.body)
        for data in datas:
            try:
                room_name = data.get('title')
                category = data.get('cate')
                auth = data.get('auth')
                hot_num = data.get('hot')
                DouYuDataModel.objects.create(room_name=room_name,
                                              category=category, auth=auth,
                                              hot_num=hot_num)
            except Exception as e:
                print('数据提取失败{},  {}'.format(data, e))
        return JsonResponse({"msg": "ok"})
